<?php
/**
 * 更新瀑布流图片宽度不对的图片
 * 
 * @author yangchao
 * @since 2012-03-19
 */


if (!file_exists("../SINASRV_CONFIG")) {
	echo "Read the sinasrv config error!\n";
//	exit();
} else {
   $array_config = parse_ini_file("../SINASRV_CONFIG", true); $_SERVER = array_merge($_SERVER, $array_config); 
}
define("CRON_NAME","cron_update_cron_update_mblog_content");//cro名称，要求唯一

set_time_limit(0);
$_GET["db_debug"] = true;
$_GET["no_pref_debug"] = true;
define("CRON_START_TIME",time());

//5分钟执行一次，遍历更新
define("PCOUNT",100);//每次处理多少mid
define("MAX_PAGE",10); //定义每个进程处理最大的页数
define("TOTAL_PROCESS",1);//添加新进程需要更新这个配置
define("CRON_TYPE","cron_update_mblog_pubu");//cron的类型
define("NOW_PROCESS",$argv[1] ? $argv[1] : 0);//当前进程ID=0 可以申请如下php xxx 1



include_once($_SERVER['DOCUMENT_ROOT'] ."/include/user/inc.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/tucao/inc.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/mblog/inc.php");


class mod_cron_engine_main extends mod_cron_engine
{
	function __construct($current_process_id)
	{
		parent::__construct($current_process_id,CRON_NAME);
	}
	
	
	function craw()
	{
		$this->get_data();
	}
	
	
	function  run()
	{
		//开始跑cron之前预先获取数据（start_id等）
		$this->before_run();
		
		//正式获取数据
		$this->craw();
		
		
		$this->after_run();
	}
	
	
	
	
	function get_data()
	{
		//这里介绍已经更新一条数据，只用于检查cron是否正常启动
		$this->replace_into_data(0);	
		
		$db_mblog_pic_content = new db_mblog_pic_content();
		
		//获取tag的总数
		$count = $db_mblog_pic_content->cron_get_data_count_width_wrong();
		
		echo "res_count:$count<br>\n";
		$total_page = ceil($count/PCOUNT);
		if($total_page > MAX_PAGE && defined("MAX_PAGE")) $total_page = MAX_PAGE;
		
		for($i=1;$i<=$total_page;$i++)
		{
			$this->get_list_by_page($i);
		}
		
		//这里介绍已经更新一条数据，只用于检查cron是否出现异常退出
		$this->replace_into_data(1);	
		
		if($count < PCOUNT)
		{
			while(1)
			{
				sleep(3);
				$this-> get_list_by_page(1);
			}
		}
			
			
		
		
		
	}
	
	
	function get_list_by_page($p)
	{
		//执行时间超过5分钟，退出
		if(time() - CRON_START_TIME > 300)
		{
			exit;
		}
		
		$db_mblog_pic_content = new db_mblog_pic_content();
		$list = $db_mblog_pic_content->cron_get_data_list_width_wrong($p,PCOUNT);
		
		foreach($list as $value)
		{
			$pic = $value["pic"];
			$id = $value["id"];
			if(!$pic)continue;
			$this->update_pic_width_by_url_and_status_id($id,$pic);
			
		}
		
	}
	
	function update_pic_width_by_url_and_status_id($id,$pic)
	{
		$db_mblog_pic_content = new db_mblog_pic_content();
		echo $pic;
		
		$res = get_image::get_image_size_by_url($pic);
		
		
		if($res["type"] == "text/html")
		{
			$db_mblog_pic_content->cron_update_mblog_info_by_id($id,"","",0);
			return;
		}
		
		$srcW = $res["width"]; 
		$srcH = $res["height"]; 
		
		$pic_width_pubu = 220;
		$pic_height_pubu = intval($srcH/($srcW/$pic_width_pubu));
		
		$db_mblog_pic_content->cron_update_mblog_info_by_id($id,$pic_width_pubu,$pic_height_pubu);
		
		
	}
	
	
	
}

$m = new mod_cron_engine_main(NOW_PROCESS);
$m->run();
